﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace TA.Turbo.Controls
{
	/// <summary>
	/// Methods common to all complex controls.
	/// </summary>
	public static class ComplexControlCommon
	{
		public static void RegisterIncludes<T>(T control, IncludeCollection includes)
			where T : TControl, IComplexControl
		{
			//foreach (ClientEvent ce in control._events.Values)
			//{
			//  if (ce is BaseServerEvent)
			//  {
			//    if (((BaseServerEvent)ce).Active)
			//    {
			//      includes.Add(Script.ServerEventSender);
			//      includes.Add(Script.EventDefinition);
			//    }
			//  }
			//}

			includes.Add(Script.ServerEventSender);
			includes.Add(Script.EventDefinition);
			includes.Add(Script.BaseComplexControl);
		}
		/// <summary>
		/// Renders the complex control.
		/// </summary>
		/// <param name="control"></param>
		/// <param name="writer"></param>
		public static void RenderControl<T>(T control, System.Web.UI.HtmlTextWriter writer)
			where T : TControl, IComplexControl
		{
			if (!control.get_IsDesignMode())
			{
				if (control.TPage != null && control.TPage.JavaScript != false)
					control.RenderInitScript(writer);
			}
		}
		/// <summary>
		/// Renders the initialization script for the specified control.
		/// </summary>
		/// <param name="control"></param>
		/// <param name="writer"></param>
		public static void RenderInitScript<T>(T control, System.Web.UI.HtmlTextWriter writer)
			where T : TControl, IComplexControl
		{
			if (!control.get_IsDesignMode())
			{
				writer.AddAttribute(HtmlTextWriterAttribute.Type, "text/javascript");
				writer.RenderBeginTag(HtmlTextWriterTag.Script);

				string varName = GetNextVarName(control);
				writer.WriteScript(@"if(typeof({0}) == ""function"")
{{
	var {2} = new {0}(""{1}"");
	{2}.Init();
"
					, control.ClientClassName
					, control.ClientID
					, varName
					);

				control.RenderInitScriptInternal(writer, varName);

				writer.WriteScript(@"
	{0}.Load();
", varName);

				control.RenderLoadScriptInternal(writer, varName);

				writer.WriteScript("}}");

				string script = control.InitControl.GetEventScript(control.ClientID);
				if (!string.IsNullOrEmpty(script))
					writer.Write(script);

				writer.RenderEndTag();
			}
		}

		/// <summary>
		/// Returns the name of the client-side class which describes this object.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="control"></param>
		/// <returns></returns>
		public static string GetClientClassName<T>(T control)
			where T : TControl, IComplexControl
		{
			return "$" + control.GetType().Name;
		}













		const string _varNameKey = "TA.Turbo.Controls.ComplexControl.VarName";


		private static string GetNextVarName<T>(T control)
			where T : TControl, IComplexControl
		{
			if (!control.Page.Items.Contains(_varNameKey))
			{
				control.Page.Items[_varNameKey] = 0;
			}
			int i = (int)control.Page.Items[_varNameKey];
			control.Page.Items[_varNameKey] = (i + 1);

			return "t" + i;
		}
		internal static void AddEventAttributes<T>(T complexControl, HtmlTextWriter writer)
			where T : TControl, IComplexControl
		{
			if (!complexControl.get_IsDesignMode())
			{
				foreach (ClientEvent e in complexControl._events.Values)
				{
					e.AddAttributes(writer);
				}
			}
		}
	}
}
